package cn.leonis.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author Tobi
 * @Date 2021-07-14 下午3:37
 * 杨辉三角
 */
public class PascalsTriangleSolution {

    public static void main(String[] args) {
        int numRows = 5;
        PascalsTriangleSolution solution = new PascalsTriangleSolution();
        List<List<Integer>> generate = solution.generate(numRows);
        for (List<Integer> list: generate) {
            for (Integer num : list) {
                System.out.printf("%d \t", num);
            }
            System.out.println();
        }
    }

    /**
     * 传入行数，生成杨辉三角
     * @param numRows
     * @return
     */
    public List<List<Integer>> generate(int numRows) {
        List<List<Integer>> result = new ArrayList<>();

        for (int i = 0; i < numRows; i++) {
            List<Integer> tempList = new ArrayList<>();
            for (int j = 0; j <= i; ++j) {
                if (j == 0 || j == i) {
                    tempList.add(1);
                }else {
                    tempList.add(result.get(i - 1).get(j - 1) + result.get(i - 1).get(j));
                }
            }
            result.add(tempList);
        }

        return result;
    }
}
